﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Kiiro.SiteDefinition.Core;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Microsoft.SharePoint;

namespace Kiiro.SiteDefinition.ProjectPages
{
    public partial class EditDocument : BasePage
    {
        public int Type // 1 = folder, anything else is document
        {
            get { return Request.QueryString [ "Type" ] == null ? 0 : Convert.ToInt32 ( Request.QueryString [ "Type" ] ); }
        }

        public string DocumentUrl
        {
            get { return Request.QueryString [ "docUrl" ] == null ? null : Convert.ToString ( Request.QueryString [ "docUrl" ] ); }
        }

        public string Source
        {
            get { return Request.QueryString [ "Source" ] ?? ServerRelativeUrl + "/Documents.aspx"; }
        }

        private Document Document = new Document ( );

        protected void Page_Load ( object sender, EventArgs e )
        {
            Document = DocumentModel.GetDocumentByUrl ( DocumentUrl, ItemId, SiteCollectionId, ServerRelativeUrl );

            if ( !Page.IsPostBack )
            {
                hlBack.NavigateUrl = Source;

                if ( Document != null )
                {
                    litTitle.Text = string.Format ( "Edit - {0}", Document.Title );
                    if ( KiiroMaster.V2Header.Project != null && !string.IsNullOrEmpty ( KiiroMaster.V2Header.Project.Title ) )
                    {
                        litTitle.Text = KiiroMaster.V2Header.Project.Title + " - " + litTitle.Text;
                    }

                    if ( Type == 1 || Document.IsFolder )
                    {
                        pnlFolder.Visible = true;
                        pnlDocument.Visible = false;
                        txtFolderName.Text = Document.Title;
                    }
                    else
                    {
                        pnlFolder.Visible = false;
                        pnlDocument.Visible = true;
                        txtDocumentTitle.Text = Document.Title.Substring ( 0, Document.Title.LastIndexOf ( '.' ) );
                    }
                }
                else
                {
                    Response.Redirect ( String.Format ( Constants.PortalFileUrls.SmartError, Request.Url.ToString ( ), "Document%20has%20been%20deleted%20or%20renamed." ) );
                }
            }
        }


        protected void btnCancel_Click ( object sender, EventArgs e )
        {
            Redirect ( new Document ( ) );
        }

        protected void btnUpload_Click ( object sender, EventArgs e )
        {
            Document doc = DocumentModel.GetDocumentById ( ItemId, SiteCollectionId, ServerRelativeUrl );
            doc.EditType = EditType.Updated;
            doc.Id = ItemId;

            Midori midori = new Midori ( );
            bool updateDoc = false;

            if ( Type == 1 || doc.IsFolder )
            {
                if ( doc.Title != txtFolderName.Text.Trim ( ) )
                {
                    doc.IsFolder = true;
                    doc.FolderUrl = doc.ServerRelativeUrl.Remove ( doc.ServerRelativeUrl.LastIndexOf ( "/" ) );
                    doc.Title = txtFolderName.Text.Trim ( );
                    doc.SiteCollectionId = Document.SiteCollectionId;
                    doc.ServerRelativeUrl = Document.ServerRelativeUrl;

                    midori.Documents.Add ( doc );
                    updateDoc = true;
                }
            }
            else
            {
                string tempTitle = doc.Title.Replace ( doc.Title.Substring ( 0, doc.Title.LastIndexOf ( "." ) ), txtDocumentTitle.Text.Trim() );
                if ( doc.Title != tempTitle )
                {
                    doc.Title = tempTitle;
                    doc.SiteCollectionId = Document.SiteCollectionId;
                    doc.ServerRelativeUrl = Document.ServerRelativeUrl;
                    doc.Url = doc.Url.Remove ( doc.Url.LastIndexOf ( "/" ) + 1 );
                    doc.FileLength = Document.FileLength;

                    midori.Documents.Add ( doc );
                    updateDoc = true;
                }
            }

            if ( updateDoc )
            {
                doc = DocumentModel.Save ( doc, null, CurrentUser, SiteCollectionId, ServerRelativeUrl );
            }
            Redirect ( doc );
        }


        private void Redirect ( Document document )
        {
            if ( document.IsValid ( ) )
            {
                Response.Redirect ( Source );
            }
            else
            {
                if ( document.IsFolder )
                    lblFolderTitleMessage.Text = FormattingHelpers.GetErrorMessageText ( document.TitleMessage );
                else
                    lblDocumentTitleMessage.Text = FormattingHelpers.GetErrorMessageText ( document.TitleMessage );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.ProjectPages/Documents/EditDocument.aspx.cs $
 * 
 * 21    2/08/10 5:04p Camerons
 * 
 * 20    1/25/10 1:17p Camerons
 * 
 * 19    1/12/10 12:03p Camerons
 * 
 * 18    26/11/09 10:35a Camerons
 * 
 * 17    17/11/09 6:46p Camerons
 * 
 * 16    10/23/09 2:31p Adamw
 * 
 * 15    10/21/09 11:44a Adamw
 * Case 350, added a trim to the input text fields, added some clearer
 * error messages to document and folder name validation, removed isFolder
 * check from IsFolderNameValid as special chars are invlid for full url
 * 
 * 14    10/19/09 12:39p Adamw
 * Case 346, changed IndexOf to LastIndexOf in PageLoad to return full
 * document name
 * 
 * 13    30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 12    30/09/09 9:52a Shafia
 * 
 * 11    22/09/09 6:03p Camerons
 * Code changes to support creating user on demand - version 1.1.x.x
 * 
 * 10    21/09/09 3:30p Camerons
 * First batch of changes for 1.1.x.x - Security groups update
 * 
 * 9     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/